This is the editing view for the Ajax data source - Popup editing example. It contains an Editor instance that will operate on a given row in a table, but it does not use a DataTable!
This example will listen for a message from the parent window, which will tell it what action to take and what row to take it on. The Editor instance will then make an Ajax request to get the data needed, display the form and allow the user to submit the data.
When the submit is done, a message is sent back to the parent window to tell it that the action is complete and this window can be closed.
It is worth noting that this example makes use of the static display controller to have the form displayed directly in the page without the need for a modal.
The JavaScript shown below is used to initialise the table shown in this example:
const editor = new DataTable.Editor({
ajax: '/api/staff',
dataSrc: 'ajax',
display: '#myForm',
fields: [
{
label: 'First name:',
name: 'first_name'
},
{
label: 'Last name:',
name: 'last_name'
},
{
label: 'Position:',
name: 'position'
},
{
label: 'Office:',
name: 'office'
},
{
label: 'Extension:',
name: 'extn'
},
{
label: 'Start date:',
name: 'start_date',
type: 'datetime'
},
{
label: 'Salary:',
name: 'salary'
}
]
});
window.addEventListener('message', function (e) {
console.log(e.data);
if (e.data.action === 'create') {
editor
.create()
.buttons('Create')
.title(e.data.title);
}
else if (e.data.action === 'edit') {
editor
.edit(e.data.id)
.buttons('Edit')
.title(e.data.title);
}
else if (e.data.action === 'remove') {
editor
.remove(e.data.id)
.on('open', () => {
console.log('vals', editor.val());
})
.buttons('Delete')
.title(e.data.title)
.message('Are you sure you wish to remove this record?');
}
editor.on('submitComplete', function () {
// Send message to parent to refresh
window.opener.postMessage({
done: true
});
});
});
In addition to the above code, the following JavaScript library files are loaded for use in this example:
The HTML shown below is the raw HTML table element, before it has been enhanced by DataTables:
This example uses a little bit of additional CSS beyond what is loaded from the library files (below), in order to correctly display the table. The additional CSS used is shown below:
The following CSS library files are loaded for use in this example to provide the styling of the table:
This table loads data by Ajax. The latest data that has been loaded is shown below. This data will update automatically as any additional data is loaded.
The script used to perform the server-side processing for this table is shown below. Please note that this is just an example script using PHP. Server-side processing scripts can be written in any language, using the protocol described in the DataTables documentation.